﻿using IPC.ListConnect.Profinet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IPC.WinForm.BaseForm
{
    public partial class FrmNewEquip : Form
    {
        public string IP { get; set; }
        public string Port {  get; set; }
        public string Slot { get; set; }
        public string Rack { get; set; }
        public string Type { get; set; }
        public string Name { get; set; }
        public string Guid { get; set; }
        public string FullName { get; set; }
        private string channelName;
        private string channelGuid;
        public FrmNewEquip(ChannelType equipType,string _channelName,string _channelGuid)
        {
            InitializeComponent();
            channelName = _channelName;
            channelGuid = _channelGuid;
            if (equipType == ChannelType.AllenBradley)
            {
                this.cbType.Items.Add("AB5000");
                this.cbType.Items.Add("AB500");
                this.textSlot.Text = "0";
                this.textRack.Text = "0";
                this.textSlot.Enabled = false;
                this.textRack.Enabled = false;
                this.txtPort.Text = "44818";
                this.cbType.SelectedIndex = -1;
            }
            else if(equipType == ChannelType.Siemens)
            {
                this.cbType.Items.Add("S7_200");
                this.cbType.Items.Add("S7_200Smart");
                this.cbType.Items.Add("S7_300");
                this.cbType.Items.Add("S7_400");
                this.cbType.Items.Add("S7_1200");
                this.cbType.Items.Add("S7_1500");
                this.txtPort.Text = "102";
                this.cbType.SelectedIndex = -1;
            }
            else
            {
                this.cbType.Items.Add("None");
                this.txtPort.Text = "0";
                this.textSlot.Text = "0";
                this.textRack.Text = "0";
                this.txtPort.Enabled = false;
                this.textSlot.Enabled = false;
                this.textRack.Enabled = false;
                this.cbType.Text = "None";
                this.cbType.SelectedIndex = 0;
                this.cbType.Enabled = false;
            }
            
        }

        public FrmNewEquip(string name,string address,string port,string slot,string rack, string equipType,string type, string _channelName,string equipGuid,string _channelGuid)
        {
            InitializeComponent();
            this.channelGuid = _channelGuid;
            channelName = _channelName;
            this.txtName.Text = name;
            this.txtIP.Text = address;
            this.txtPort.Text= port;
            this.textSlot.Text = slot;
            this.textRack.Text = rack;
            Guid = equipGuid;
            if (equipType == ChannelType.AllenBradley.ToString())
            {
                this.cbType.Items.Add("AB5000");
                this.cbType.Items.Add("AB500");
                this.textSlot.Text = "0";
                this.textRack.Text = "0";
                this.txtPort.Text = "44818";
                this.cbType.SelectedIndex = -1;
            }
            else if (equipType == ChannelType.Siemens.ToString())
            {
                this.cbType.Items.Add("S7_200");
                this.cbType.Items.Add("S7_200Smart");
                this.cbType.Items.Add("S7_300");
                this.cbType.Items.Add("S7_400");
                this.cbType.Items.Add("S7_1200");
                this.cbType.Items.Add("S7_1500");
                this.txtPort.Text = "102";
                this.cbType.SelectedIndex = -1;
            }
            else
            {
                this.cbType.Items.Add("None");
                this.txtPort.Text = "0";
                this.textSlot.Text = "0";
                this.textRack.Text = "0";
                this.txtPort.Enabled = false;
                this.textSlot.Enabled = false;
                this.textRack.Enabled = false;
                this.cbType.Text = "None";
                this.cbType.SelectedIndex = 0;
                this.cbType.Enabled = false;
            }
            this.cbType.Text = type;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(this.txtName.Text))
            {
                MessageBox.Show("请填写设备名称");
                return;
            }
            string ip = this.txtIP.Text;
            if (!ToolUtil.ValidateIPAddress(ip))
            {
                MessageBox.Show("请输入正确的IP地址");
                return;
            }
            if(!int.TryParse(this.txtPort.Text,out int port))
            {
                MessageBox.Show("请输入正确的端口号");
                return;
            }
            if(!short.TryParse(this.textSlot.Text,out short slot))
            {
                MessageBox.Show("请输入正确的槽口号");
                return;
            }
            if (!short.TryParse(this.textRack.Text, out short rack))
            {
                MessageBox.Show("请输入正确的架子号");
                return;
            }
            if(this.cbType.SelectedIndex == -1 && this.cbType.Enabled)
            {
                MessageBox.Show("请选择型号");
                return;
            }
            Name = this.txtName.Text;
            IP = this.txtIP.Text;
            Port = this.txtPort.Text;
            Slot = this.textSlot.Text;
            Rack = this.textRack.Text;
            Type = this.cbType.Text;
            if(this.Guid == null || this.Guid == string.Empty)
            {
                Guid = System.Guid.NewGuid().ToString("N");
            }
            
            FullName = channelGuid + "." + Guid;
            this.DialogResult = DialogResult.OK;
        }


    }
}
